package com.swm.julie.rest.sausage.rpc.client;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.junit.Test;

import com.swm.julie.rest.sausage.index.field.Product;

public class XMLRPCSearchImpl implements XMLRPCSearch {

	String sausageRPCServerURL = "http://localhost:8888/SausageRPCServer/xmlrpc";
	
	@Test
	public void search() throws MalformedURLException, XmlRpcException
	{
		standardSearch("청담");
		itemSearch("패딩");
		companySearch("coupang");
		itemCompanySearch("coupang", "엔제리너스");
		provinceSearch("Seoul");
	}

	@Override
	public void standardSearch(String query) throws MalformedURLException, XmlRpcException 
	{
		// TODO Auto-generated method stub
		System.out.println("XMLRPCSearchImpl.standardSearch()");
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		
		
		Object[] rest = (Object[]) client.execute("SausageSearcher.standardSearch", params);
		
		for(Object object : (Object[]) rest)
		{
			Product product = (Product) object;
			
			System.out.println("result : " + product.getItemName());
		}
	}
	
	public void itemSearch(String query) throws MalformedURLException, XmlRpcException
	{
		System.out.println("XMLRPCSearchImpl.itemSearch()");
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.itemSearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			
			System.out.println("result : " + product.getItemName());
		}
	}
	
	@Override
	public void companySearch(String query) throws MalformedURLException, XmlRpcException 
	{
		System.out.println("XMLRPCSearchImpl.companySearch()");
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.companySearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			
			System.out.println("result : " + product.getItemName());
		}
	}
	
	@Override
	public void itemCompanySearch(String company, String query) throws MalformedURLException, XmlRpcException 
	{
		System.out.println("XMLRPCSearchImpl.itemCompanySearch()");
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{company, query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.itemCompanySearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			
			System.out.println("result : " + product.getItemName());
		}
	}
	
	public void provinceSearch(String query) throws MalformedURLException ,XmlRpcException 
	{
		System.out.println("XMLRPCSearchImpl.provinceSearch()");
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.provinceSearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			
			System.out.println("result : " + product.getItemName());
		}
	}
}
